<?php

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class DBQueryListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {
        try {
            if (config('app.debug') == true) {
                $sql = str_replace('?', "'%s'", $event->sql);
                $log = vsprintf($sql, $event->bindings);
                $this->put_log('sql', $log);
            }
        } catch (\Exception $exception) {

        }
    }

    /**
     * 写入日志
     * @param string $file
     * @param string $content
     */
    private function put_log($file = 'app', $content = '')
    {
        $data = date('Y-m-d');
        $cut_line = str_repeat("-", 100);
        is_dir(storage_path('logs/sql')) or mkdir(storage_path('logs/sql'), 0777, true); // 文件夹不存在则创建
        $content = '[' . date('Y-m-d H:i:s') . "]" . $content;
        @file_put_contents(storage_path('logs/sql/' . $file . '-' . $data . '.log'), $content . "\n" . $cut_line . "\n\n", FILE_APPEND);
    }
}
